package com.seuic.hnjiningposapp.hnapps.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.orhanobut.logger.Logger;
import com.seuic.hnjiningposapp.R;
import com.seuic.hnjiningposapp.hnapps.base.BaseActivity;
import com.seuic.hnjiningposapp.hnapps.ui.adapter.ItemGridMainAdapter;
import com.seuic.hnjiningposapp.hnapps.util.HttpUtils;
import com.seuic.hnjiningposapp.hnapps.util.MoneyUtils;
import com.seuic.hnjiningposapp.hnapps.util.PrintUtil;
import com.seuic.posdsdk.pos_func.PosFuncs;
import com.socks.library.KLog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * isjingkai
 * 2019年7月20日10:40:27
 * 首页
 *
 * @author rainking
 */
public class HomeActivity extends BaseActivity {

    @BindView(R.id.rv_list)
    RecyclerView mRvList;
    @BindView(R.id.base_back_btn)
    LinearLayout baseBackBtn;
    @BindView(R.id.back_tv)
    TextView backTv;

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_home;
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        this.setTitleText("惠农金服APP");
        List<ItemGridMainAdapter.GridMain> mGridMains = new ArrayList<>();
        mGridMains.add(new ItemGridMainAdapter.GridMain(R.drawable.tohelpfarmrs_withdrawals, "助农取款", ToHelpFarmersWithdrawMoneyActivity.class));
        mGridMains.add(new ItemGridMainAdapter.GridMain(R.drawable.cash_remittance, "现金汇款", CashRemittanceActivity.class));
        mGridMains.add(new ItemGridMainAdapter.GridMain(R.drawable.remittance_transfer, "转账汇款", TransferActivity.class));
        mGridMains.add(new ItemGridMainAdapter.GridMain(R.drawable.inquiry_business, "查询业务", QuiryBusinessActivity.class));
        mGridMains.add(new ItemGridMainAdapter.GridMain(R.drawable.constantrotation, "定活互转", ConstantrotationActivity.class));
        mGridMains.add(new ItemGridMainAdapter.GridMain(R.drawable.selfhelpcontract, "自助签约", SelfHelpContractActivity.class));
        mGridMains.add(new ItemGridMainAdapter.GridMain(R.drawable.ecash, "电子现金", EcashActivity.class));
        mGridMains.add(new ItemGridMainAdapter.GridMain(R.drawable.special_service, "特殊业务", SpecialBusinessActivity.class));
        mGridMains.add(new ItemGridMainAdapter.GridMain(R.drawable.operator_service, "操作员业务", OperatorBusinessActivity.class));

        ItemGridMainAdapter mItemGridMainAdapter = new ItemGridMainAdapter(mGridMains);

        mRvList.setLayoutManager(new GridLayoutManager(this
                , 3));
        mRvList.setAdapter(mItemGridMainAdapter);
        mItemGridMainAdapter.setOnItemClickListener((adapter, view, position) -> {
            ItemGridMainAdapter.GridMain gridMain = (ItemGridMainAdapter.GridMain) adapter.getData().get(position);
            Class<? extends BaseActivity> cls = gridMain.getCls();
            if (cls != null) {
                ActivityUtils.startActivity(cls);
            } else {
                ToastUtils.showLong("该功能尚未开通，敬请期待");
            }

        });
        backTv.setText("签退");
        baseBackBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(HomeActivity.this, ActivateActivity.class)
                        .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
            }
        });
    }


    @Override
    public void initEvent(Bundle savedInstanceState) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    //声明一个long类型变量：用于存放上一点击“返回键”的时刻
    private long mExitTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断用户是否点击了“返回键”
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //与上次点击返回键时刻作差
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                //大于2000ms则认为是误操作，使用Toast进行提示
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                //并记录下本次点击“返回键”的时刻，以便下次进行判断
                mExitTime = System.currentTimeMillis();
            } else {
                //小于2000ms则认为是用户确实希望退出程序-调用System.exit()方法进行退出
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
